import {
	login,
	logout,
	getInfo
} from '@/api/lsh/login.js'
import {
	getToken,
	setToken,
} from '@/utils/auth'
import router, {
	resetRouter
} from '@/router'

import route from 'vue-router'
import {
	Message
} from 'element-ui'

const state = {
	token: getToken(),
	name: '',
	avatar: '',
	roles: [],
	menus: [],
	id: '',
}
import Cookies from 'js-cookie'

const mutations = {
	SET_TOKEN: (state, token) => {
		setToken(token)
		state.token = token
	},
	SET_NAME: (state, name) => {
		state.name = name
	},
	SET_ROLES: (state, roles) => {
		state.roles = roles
	},
	SET_MENUS: (state, menus) => {
		state.menus = menus
	},
	SET_ID: (state, id) => {
		state.id = id
	},
	SET_DEPARTMENTID: (state, departmentId) => {
		state.departmentId = departmentId
	},
}

const actions = {
	// user login
	login({
		commit
	}, userInfo) {
		const {
			username,
			password
		} = userInfo
		return new Promise((resolve, reject) => {
			login({
				username: username.trim(),
				password: password
			}).then(response => {
				const {
					data
				} = response
				commit('SET_TOKEN', data.token)
				resolve(data)
			}).catch(error => {
				reject(error)
			})
		})
	},

	// get user info
	getInfo({
		commit,
		state
	}) {
		return new Promise((resolve, reject) => {
			getInfo(state.token).then(response => {
				const {
					data
				} = response
				if (!data) {
					reject('Verification failed, please Login again.')
				}
				data.roles = [data.departmentName]
				// roles must be a non-empty array
				if (!data.departmentName) {
					reject('getInfo: roles must be a non-null array!')
				}
				// commit('SET_TOKEN', data.token)
				commit('SET_ID', data.id)
				commit('SET_ROLES', [data.departmentName])
				commit('SET_NAME', data.nickname)
				//这里是判断type为1的时候，也就是部位功能菜单的时候才放进菜单数组
				var arr = []
				for (var i = 0; i < data.permissionCodes.length; i++) {
					var item = data.permissionCodes[i]
					if (item.type === 1) {
						arr.push(item)
					}
				}
				commit('SET_MENUS', arr)
				commit('SET_DEPARTMENTID', data.departmentId)
				resolve(data)
			}).catch(error => {
				reject(error)
			})
		})
	},

	// user logout
	logout({
		commit,
		state,
		dispatch
	}) {
		return new Promise((resolve, reject) => {
			logout(state.token).then(() => {
				commit('SET_TOKEN', '')
				commit('SET_ROLES', [])
				commit('SET_MENUS', [])
				commit('SET_ID', '')
				commit('SET_DEPARTMENTID', '')
				resetRouter()
				// setProject('')
				// reset visited views and cached views
				// to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
				// dispatch('tagsView/delAllViews', null, {
				// root: true
				// })
				resolve()
			}).catch(error => {
				reject(error)
			})
		})
	},

	// remove token
	resetToken({
		commit
	}) {
		return new Promise(resolve => {
			commit('SET_TOKEN', '')
			commit('SET_ROLES', [])
			commit('SET_ID', '')
			commit('SET_DEPARTMENTID', '')
			resolve()
		})
	},

	// dynamically modify permissions
	async changeRoles({
		commit,
		dispatch
	}, role) {
		const token = role + '-token'

		commit('SET_TOKEN', token)

		const {
			roles
		} = await dispatch('getInfo')

		resetRouter()

		// generate accessible routes map based on roles
		const accessRoutes = await dispatch('permission/generateRoutes', roles, {
			root: true
		})
		// dynamically add accessible routes
		router.addRoutes(accessRoutes)

		// reset visited views and cached views
		dispatch('tagsView/delAllViews', null, {
			root: true
		})
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
